/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main_frame_admin.java
 *
 * Created on 15.02.2012, 19:59:18
 */

package pazi_laba1;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mike
 */
public class Main_frame_admin extends javax.swing.JFrame {

    /** Creates new form Main_frame_admin */
    public Main_frame_admin() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList(new Admin_handler().L_M("conf.cfg"));
        jButton_create_user = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextField_new_username = new javax.swing.JTextField();
        jButton_ok_newuser = new javax.swing.JButton();
        jButton_refreshlist = new javax.swing.JButton();
        jCheckBox_shift_pass = new javax.swing.JCheckBox();
        jCheckBox_numbers = new javax.swing.JCheckBox();
        jCheckBox3_repeatsign = new javax.swing.JCheckBox();
        jTextField_passlenght = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList2ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        jButton_create_user.setText("Создать пользователя");
        jButton_create_user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_create_userActionPerformed(evt);
            }
        });

        jButton2.setText("Блокировать");

        jButton3.setText("Разблокировать");

        jTextField_new_username.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        jTextField_new_username.setForeground(new java.awt.Color(255, 51, 0));
        jTextField_new_username.setText("Введите имя нового пользователя");
        jTextField_new_username.setVisible(false);
        jTextField_new_username.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField_new_usernameFocusGained(evt);
            }
        });

        jButton_ok_newuser.setText("Создать");
        jButton_ok_newuser.setVisible(false);
        jButton_ok_newuser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ok_newuserActionPerformed(evt);
            }
        });

        jButton_refreshlist.setText("Обновить");
        jButton_refreshlist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_refreshlistActionPerformed(evt);
            }
        });

        jCheckBox_shift_pass.setText("Строчные прописные буквы");
        jCheckBox_shift_pass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_shift_passActionPerformed(evt);
            }
        });

        jCheckBox_numbers.setText("Цифры и знаки ");
        jCheckBox_numbers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_numbersActionPerformed(evt);
            }
        });

        jCheckBox3_repeatsign.setText("Повторяющиеся символы");
        jCheckBox3_repeatsign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox3_repeatsignActionPerformed(evt);
            }
        });

        jTextField_passlenght.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_passlenghtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 537, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton_create_user)
                    .addComponent(jTextField_new_username, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_ok_newuser))
                .addGap(118, 118, 118)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox3_repeatsign)
                    .addComponent(jCheckBox_numbers)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox_shift_pass)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_passlenght, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                .addGap(104, 104, 104))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(407, Short.MAX_VALUE)
                .addComponent(jButton_refreshlist)
                .addGap(196, 196, 196))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jButton_refreshlist)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton_create_user)
                        .addGap(26, 26, 26)
                        .addComponent(jTextField_new_username, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(jButton_ok_newuser))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton3)
                            .addComponent(jButton2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox_shift_pass)
                    .addComponent(jTextField_passlenght, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox_numbers)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox3_repeatsign)
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_create_userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_create_userActionPerformed
        // TODO add your handling code here:
        jTextField_new_username.setVisible(true);
        jButton_ok_newuser.setVisible(true);
    }//GEN-LAST:event_jButton_create_userActionPerformed

    private void jTextField_new_usernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField_new_usernameFocusGained
        // TODO add your handling code here:
        jTextField_new_username.setText("");
        jTextField_new_username.setForeground(Color.black);
    }//GEN-LAST:event_jTextField_new_usernameFocusGained

    private void jButton_ok_newuserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ok_newuserActionPerformed
        // TODO add your handling code here:
      new Admin_handler().Add_user("conf.cfg", jTextField_new_username.getText());
      jTextField_new_username.setVisible(false);
      jButton_ok_newuser.setVisible(false);
    }//GEN-LAST:event_jButton_ok_newuserActionPerformed

    private void jButton_refreshlistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_refreshlistActionPerformed
        // TODO add your handling code here:
        jList2 = new javax.swing.JList(new Admin_handler().L_M("conf.cfg"));
        jScrollPane2.setViewportView(jList2);
    }//GEN-LAST:event_jButton_refreshlistActionPerformed

    private void jCheckBox3_repeatsignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3_repeatsignActionPerformed
        // TODO add your handling code here:
           String rules="";
        rules+=jTextField_passlenght.getText();
        if(jCheckBox_shift_pass.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox_numbers.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox3_repeatsign.isSelected()){rules+="1";}else{rules+="0";}
        System.out.println(rules);
        rules="";
    }//GEN-LAST:event_jCheckBox3_repeatsignActionPerformed

    private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged
        // TODO add your handling code here:
        String selected=jList2.getSelectedValue().toString();
        selected=selected.substring(0, 0+selected.indexOf("-"));
        String rules=new Admin_handler().Read_rules("conf.cfg", selected);
        jTextField_passlenght.setText(Character.toString(rules.charAt(0)));
        if(rules.charAt(1)=='1'){jCheckBox_shift_pass.setSelected(true);}else{jCheckBox_shift_pass.setSelected(false);}
        if(rules.charAt(2)=='1'){jCheckBox_numbers.setSelected(true);}else{jCheckBox_numbers.setSelected(false);}
        if(rules.charAt(3)=='1'){jCheckBox3_repeatsign.setSelected(true);}else{jCheckBox3_repeatsign.setSelected(false);}
    }//GEN-LAST:event_jList2ValueChanged

    private void jCheckBox_shift_passActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_shift_passActionPerformed
        // TODO add your handling code here:
        String selected=jList2.getSelectedValue().toString();
        selected=selected.substring(0, 0+selected.indexOf("-"));
        String rules="";
        rules+=jTextField_passlenght.getText();
        if(jCheckBox_shift_pass.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox_numbers.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox3_repeatsign.isSelected()){rules+="1";}else{rules+="0";}
        System.out.println(rules);   
        new Admin_handler().Change_rules("conf.cfg", selected, rules);
        rules="";
    }//GEN-LAST:event_jCheckBox_shift_passActionPerformed

    private void jCheckBox_numbersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_numbersActionPerformed
        // TODO add your handling code here:
        String selected=jList2.getSelectedValue().toString();
        selected=selected.substring(0, 0+selected.indexOf("-"));
           String rules="";
        rules+=jTextField_passlenght.getText();
        if(jCheckBox_shift_pass.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox_numbers.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox3_repeatsign.isSelected()){rules+="1";}else{rules+="0";}
        System.out.println(rules);
        new Admin_handler().Change_rules("conf.cfg", selected, rules);
        rules="";
    }//GEN-LAST:event_jCheckBox_numbersActionPerformed

    private void jTextField_passlenghtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_passlenghtActionPerformed
        // TODO add your handling code here:
        String selected=jList2.getSelectedValue().toString();
        selected=selected.substring(0, 0+selected.indexOf("-"));
         String rules="";
        rules+=jTextField_passlenght.getText();
        if(jCheckBox_shift_pass.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox_numbers.isSelected()){rules+="1";}else{rules+="0";}
        if(jCheckBox3_repeatsign.isSelected()){rules+="1";}else{rules+="0";}
        System.out.println(rules);
        new Admin_handler().Change_rules("conf.cfg", selected, rules);
        rules="";
    }//GEN-LAST:event_jTextField_passlenghtActionPerformed

    /**
    * @param args the command line arguments
    */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main_frame_admin().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton_create_user;
    private javax.swing.JButton jButton_ok_newuser;
    private javax.swing.JButton jButton_refreshlist;
    private javax.swing.JCheckBox jCheckBox3_repeatsign;
    private javax.swing.JCheckBox jCheckBox_numbers;
    private javax.swing.JCheckBox jCheckBox_shift_pass;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField_new_username;
    private javax.swing.JTextField jTextField_passlenght;
    // End of variables declaration//GEN-END:variables

}
